package org.tinaelle.client.gui.grid;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.core.Position;
import com.gwtext.client.data.Record;
import com.gwtext.client.data.Store;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.form.Form;
import com.gwtext.client.widgets.form.FormPanel;

public class DataDetail<T> extends Panel {
	protected final FormPanel	formPanel;
	private Record	            currentRecord;
	private final DataDetailItf	dataDetailItf;

	public DataDetail(final Store store, final DynaTableDataProvider<T> dataProvider, final DataDetailItf dataDetailItf) {
		this.dataDetailItf = dataDetailItf;
		setBorder( false );
		setPaddings( 15 );

		formPanel = new FormPanel();
		formPanel.setFrame( true );
		formPanel.setLabelAlign( Position.LEFT );
		// formPanel.setPaddings( 5 );
		formPanel.setAutoWidth( true );

		formPanel.add( dataDetailItf.getFieldPanel() );

		final Button resetButton = new Button( "Reset" );
		resetButton.setIconCls( "reset-icon" );
		resetButton.addListener( new ButtonListenerAdapter() {
			@Override
			public void onClick(final Button button, final EventObject e) {
				formPanel.getForm().loadRecord( currentRecord );
			}
		} );

		final Button newButton = new Button( "New" );
		newButton.setIconCls( "new-icon" );
		newButton.addListener( new ButtonListenerAdapter() {
			@Override
			public void onClick(final Button button, final EventObject e) {
				final Record newRecord = dataProvider.getNewRecord();
				store.add( newRecord );
				setCurrentRecord( newRecord );
			}
		} );

		final Button saveButton = new Button( "Save" );
		saveButton.setIconCls( "save-icon" );
		saveButton.addListener( new ButtonListenerAdapter() {
			@Override
			public void onClick(final Button button, final EventObject e) {
				formPanel.getForm().updateRecord( currentRecord );
				currentRecord.getId();
				dataProvider.saveOrUpdateRecord( currentRecord, new AsyncCallback<T>() {
					public void onFailure(final Throwable caught) {
						// rowDataAcceptor.failed( caught );
					} // end onFailure()

					public void onSuccess(final T result) {
						final Record updatedRecord = dataProvider.convertToRecord( result );
						setCurrentRecord( updatedRecord );
						store.commitChanges();
					} // end onSuccess()
				} // end new
				        );
			}
		} );

		final Button deleteButton = new Button( "Delete" );
		deleteButton.setIconCls( "delete-icon" );
		deleteButton.addListener( new ButtonListenerAdapter() {
			@Override
			public void onClick(final Button button, final EventObject e) {
				dataProvider.deleteRecord( currentRecord, new AsyncCallback<Void>() {
					public void onFailure(final Throwable caught) {
						// rowDataAcceptor.failed( caught );
					} // end onFailure()

					public void onSuccess(final Void result) {
						store.remove( currentRecord );
						setCurrentRecord( null );
					} // end onSuccess()
				} // end new
				        );
				formPanel.getForm().reset();
			}
		} );

		formPanel.addButton( resetButton );
		formPanel.addButton( newButton );
		formPanel.addButton( saveButton );
		formPanel.addButton( deleteButton );

		add( formPanel );
	}

	public void setCurrentRecord(final Record currentRecord) {
		this.currentRecord = currentRecord;
		if ( currentRecord != null ) {
			dataDetailItf.preloadCombox( currentRecord );
		}
		formPanel.getForm().loadRecord( currentRecord );
	}

	public Form getForm() {
		return formPanel.getForm();
	}
}
